package com.dongnam.quanly.common.controller;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.ActionCategory;
import com.dongnam.quanly.common.ActionType;
import com.dongnam.quanly.common.ObjectType;
import com.dongnam.quanly.common.ScreenId;
import com.dongnam.quanly.common.dao.AuditTrailLogDao;
import com.dongnam.quanly.common.exceptions.DatabaseException;
import com.dongnam.quanly.common.model.AuditTrailLog;
import com.dongnam.quanly.common.persistence.HibernateUtil;

public class AuditTrailLogController extends GenericController<AuditTrailLog, Long> {

	private static final Logger logger = LoggerFactory.getLogger(AuditTrailLogController.class);

	public AuditTrailLogController() {
		super(new AuditTrailLogDao());
	}

	@Override
	public AuditTrailLogDao getDao() {
		return (AuditTrailLogDao) dao;
	}

	public long findCountByFilters(Timestamp fromTime, Timestamp toTime, String actioneeName, String actioneeIp,
			ScreenId screenId, ActionCategory actionCategory, ActionType actionType, String result, ObjectType object,
			String objectId) throws DatabaseException {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		long count;
		try {
			count = getDao().findCountByFilters(fromTime, toTime, actioneeName, actioneeIp, screenId, actionCategory,
					actionType, result, object, objectId);
			if (!isTransacted) {
				session.getTransaction().commit();
			}
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}
		return count;

	}

	public List<AuditTrailLog> findByFilters(int pageSize, int pageNumber, Timestamp fromTime, Timestamp toTime,
			String actioneeName, String actioneeIp, ScreenId screenId, ActionCategory actionCategory,
			ActionType actionType, String result, ObjectType object, String objectId, String sortField, String sortOrder)
			throws DatabaseException {
		logger.debug("findByFilters, pageSize: " + pageSize);
		List<AuditTrailLog> list = null;
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();

		try {
			logger.debug("load (pageSize = {}, sortField = {}, sortOrder = {})", new Object[] { pageSize, sortField,
					sortOrder });
			list = getDao().findByFilters(pageSize, pageNumber, fromTime, toTime, actioneeName, actioneeIp, screenId,
					actionCategory, actionType, result, object, objectId, sortField, sortOrder);
			session.getTransaction().commit();
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}
		return list;
	}
}
